<?php

namespace app\index\service;
use think\Db;
//支付服务层
class PayService{

	//余额支付
	public function payyue($parm){

		$order = Db::table("ys_order")->where("id",$parm["orderid"])->find();
		//dd($order);//user_money
		if($order["pay_status"]==1){
			//重复付钱
			return 1;
		}
		if($parm["user"]["user_money"]<$order["final_amount"]){
			//月不足
			return 2;
		}
		//用户金额改变
		//消费总额度
		$total = Db::table("ys_user")->where('uid', $parm['uid'])->setInc('total_amount', $order["final_amount"]);
		//减钱
		$final = Db::table("ys_user")->where('uid', $parm['uid'])->setDec('user_money', $order["final_amount"]);
		//加积分
		$poin  = Db::table("ys_user")->where('uid', $parm['uid'])->setInc('pay_points', ceil($order["final_amount"]/10));
	    //钱包日志
	   	$user_wallet_log = [
	   			"uid"			=>$parm['uid']  ,
	   			"admin_id"		=> '' ,							//管理员ID，后台变更填写，前台变更不填写
	   			"store_id"		=> $order["store_id"] ,			//门店id
	   			"order_id"		=> $parm["orderid"] ,   		//订单id
	   			"order_sn"		=> $order["order_sn"] ,			//订单编号			
	   			"uname"			=> $parm["user"]["realname"],
	   			"amount"		=> $order["final_amount"] , 	//变更值
	   			"cur_amount"	=> $parm["user"]["user_money"] ,
	   			"sign"			=>  2,
	   			"chargetype"	=> 1 , 							//1消费，2充值
	   			"charge_cn"		=>"余额购买"  , 				 //备注
	   			"addtime"		=>time() 
	   		];
	    Db::table("ys_user_wallet_log")->insertGetId($user_wallet_log);
		//订单状态
		 $ordercationdata =[
	 			"uid" =>$parm["uid"],        		//下单人id
	 			"order_id" =>$parm["orderid"],       //订单id
	 			"action_user" =>0,          		 //操作人 0 为用户操作，其他为管理员id
	 			"order_status" =>0,         		 //订单状态
	 			"pay_status" =>1,           		 //支付状态
	 			"status_desc" =>"余额付款",		//状态描述
	 			"storeid" =>$order["store_id"], 		//门店id
	 			"addtime" =>time()
		 	];
		Db::table("ys_order_action")->insertGetId($ordercationdata);
		//订单状态改变
		$updata = ["pay_status"=>1,"order_status"=>1];
		$updat = Db::table("ys_order")->where("id",$parm["orderid"])->update($updata);
	   	if($total&&$final&&$poin&&$updat){
	   	 	return 3;	
	   	}
	   
		
	}

}